package com.roy.wakeupres.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.roy.wakeupres.entity.ButterflyTask;
import com.roy.wakeupres.query.ButtonQuery;
import com.roy.wakeupres.service.IButterflyTaskService;
import com.roy.wakeupres.utils.AjaxResult;
import com.roy.wakeupres.vo.ButterflyTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Roy
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/butterflyTask")
public class ButterflyTaskController {

    @Autowired
    private IButterflyTaskService service;
//
//    @Autowired
//    private ButterflyModelMapper butterflyModelMapper;

    @GetMapping("getTasks")
    @CrossOrigin
    public AjaxResult getTasks(){

        // 获取当前日期的起始时间
        LocalDateTime startDate = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LambdaQueryWrapper<ButterflyTask> qw = new LambdaQueryWrapper<>();
        qw.ge(ButterflyTask::getCreateTime , startDate);
        List<ButterflyTask> list = service.list(qw);
        List<ButterflyTaskVo> butterflyTaskVos = BeanUtil.copyToList(list, ButterflyTaskVo.class);
        for (ButterflyTaskVo butterflyTaskVo : butterflyTaskVos) {
            if(butterflyTaskVo.getTaskStatus() == 1){
                butterflyTaskVo.setAutoStart(true);
            }
        }
        return AjaxResult.success(butterflyTaskVos);
    }

    /**
     * 开始、暂停、结束
     */
    @PostMapping("button")
    @CrossOrigin
    public AjaxResult button(@RequestBody ButtonQuery query){

        service.button(query);
        return AjaxResult.success();
    }

    /**
     * 开始、暂停、结束
     */
    @GetMapping("test")
    @CrossOrigin
    public AjaxResult button(){

        service.init();
        return AjaxResult.success();
    }
//
//    /**
//     * 开始、暂停、结束
//     */
//    @GetMapping("test1")
//    @CrossOrigin
//    public AjaxResult test1(){
//
//        List<ButterflyTask> tasks = Json2Object.getTask();
//        List<ButterflyModel> butterflyModels = BeanUtil.copyToList(tasks, ButterflyModel.class);
//        for (ButterflyModel butterflyModel : butterflyModels) {
//            butterflyModelMapper.insert(butterflyModel);
//        }
//        return AjaxResult.success();
//    }


}
